package co.com.capsula.pages;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Service;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.PasswordField;
import org.apache.tapestry5.ioc.annotations.Inject;

import co.lifeproy.service.UserManager;

public class FormLogin {
	
    @Component
    private Form form;
    
    @Component(id = "password")
    private PasswordField passwordField;
	
	private String login;

	@Inject
	@Service("userManager")
	private UserManager userManager;

	private String password;

	public String getLogin() {
		return login;
	}

	@Validate("required")
	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	@Validate("required")
	public void setPassword(String password) {
		this.password = password;
	}

	String onSuccess() {
		String ret = "FormLogin";
		if (userManager.checkLogin(login, password)) {
			ret = "Index";
		} else {
			form.recordError(passwordField, "Nombre de usuario o password no valido.");
		}
		return ret;
	}
}
